<!doctype html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="../../vender/animate@3.5.0.min.css">
    <style>
        .color-preview {
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>

<div id="app">
    <input type="text" v-model="colorQuery" @keyup.enter="updateColor" placeholder="Enter a color">
    <button @click="updateColor">Update</button>
    <p>Preview:</p>
    <div :style="{background:tweenedCSSColor}" class="color-preview"></div>
    <p>tweenedColor:{{tweenedCSSColor}}</p>
</div>


</body>
<script src="../../vender/vue@2.4.2.js"></script>
<script src="https://cdn.jsdelivr.net/npm/tween.js@16.3.4"></script>
<script src="https://cdn.jsdelivr.net/npm/color-js@1.0.3"></script>
<script>
    var Color=net.brehaut.Color;

    var app=new Vue({
        el:'#app',
        data:{
            colorQuery:'#000',
            color:{
                red:0,
                green:0,
                blue:0,
                alpha:1
            },
            tweenedColor:{}
        },
        created: function () {
            this.tweenedColor=Object.assign({},this.color);
        },
        watch: {
            color:function () {
                var vm=this;
                function animate(){
                    if(TWEEN.update()){
                        requestAnimationFrame(animate);
                    }
                }
                new TWEEN.Tween(this.tweenedColor)
                        .to(this.color,750)
                        .start();
                animate();
            }
        },
        computed:{
            tweenedCSSColor: function () {
                return new Color({
                    red:this.tweenedColor.red,
                    green:this.tweenedColor.green,
                    blue:this.tweenedColor.blue,
                    alpha:this.tweenedColor.alpha
                }).toCSS();
            }
        },
        methods:{
            updateColor: function () {
                this.color=new Color(this.colorQuery).toRGB();
                this.queryColor='';
            }
        }
    });

</script>
</html>